package com.laiyima.zhongjiang.linghuilv.demo.shop;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.laiyima.zhongjiang.linghuilv.demo.R;
import com.laiyima.zhongjiang.linghuilv.demo.adapter.ImageAdapter;
import com.laiyima.zhongjiang.linghuilv.demo.bean.DataBean;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnPageChangeListener;

public class ShoppingActivity extends AppCompatActivity implements OnPageChangeListener {
    private WebView webView;
      private RelativeLayout webParentView;
    private View mErrorView;
    private String TAG="ShoppingActivity";
    Banner banner;
    private LinearLayout linearLayout;
    private static final int TIME_EXIT=3000;
    private static int isExit=0;
    //实现按两次后退才退出
    private CountDownTimer mCountDownTimer = new CountDownTimer(TIME_EXIT,1000) {
        @Override
        public void onTick(long l) {
            isExit++;

        }

        @Override
        public void onFinish() {
            isExit=0;
        }
    };

    public boolean onKeyDown(int keyCode, KeyEvent event){
        if(keyCode==KeyEvent.KEYCODE_BACK){
            exit();
            return true;
        }
        return false;
    }
    private void exit(){
        if(isExit<1){
            Toast.makeText(getApplicationContext(),"再点击一次返回退出程序",Toast.LENGTH_SHORT).show();
            mCountDownTimer.start();
        }else{
            isExit=0;
            Toast.makeText(getApplicationContext(),"退出程序",Toast.LENGTH_SHORT).show();
            mCountDownTimer.cancel();
            Intent startMain = new Intent(Intent.ACTION_MAIN);
            startMain.addCategory(Intent.CATEGORY_HOME);
            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(startMain);
            System.exit(0);
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping);
        linearLayout=findViewById(R.id.banner_background);
        banner=findViewById(R.id.banner);
        banner.setAdapter(new ImageAdapter(DataBean.getTestData()));
        //设置指示器
        banner.setIndicator(new CircleIndicator(this));
        //设置点击事件
        //  banner.setOnBannerListener((data, position) -> {
        //  Snackbar.make(banner, ((DataBean) data).title, Snackbar.LENGTH_SHORT).show();
        //      });
        //添加切换监听
         banner.addOnPageChangeListener(this);
        //圆角
        banner.setBannerRound(10);
        //  Pager=findViewById(R.id.myViewPager);
        //intViewPsger();


        webView=findViewById(R.id.shop_web);
        webView.setWebViewClient(new WebViewClient());
       // webView.loadUrl("http://shop.laiima.com/app/index.php?i=10&c=entry&m=ewei_shopv2&do=mobile");
        WebSettings settings = webView.getSettings();
        settings.setDomStorageEnabled(true);

        initWebView();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if(position==0){
            linearLayout.setBackgroundColor(Color.parseColor("#315BC9"));
        }
        if(position==1){
            linearLayout.setBackgroundColor(Color.parseColor("#A8732E"));
        }
        if(position==2){
            linearLayout.setBackgroundColor(Color.parseColor("#1C18CD"));
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    private void initWebView() {
        //加载需要显示的网页
        webView.loadUrl("http://shop.laiima.com/app/index.php?i=10&c=entry&m=ewei_shopv2&do=mobile");
        WebSettings mWebSettings = webView.getSettings();
        mWebSettings.setJavaScriptEnabled(true);    //允许加载javascript
        mWebSettings.setSupportZoom(true);          //允许缩放
        mWebSettings.setBuiltInZoomControls(true);  //原网页基础上缩放
        mWebSettings.setUseWideViewPort(true);      //任意比例缩放
        mWebSettings.setLoadWithOverviewMode(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                //6.0以下执行
                Log.i(TAG, "onReceivedError: ------->errorCode" + errorCode + ":" + description);
                //网络未连接
              //  showErrorPage();
            }

            //处理网页加载失败时
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                //6.0以上执行
                Log.i(TAG, "onReceivedError: ");
             //   showErrorPage();//显示错误页面
            }
        });

        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                Log.i(TAG, "onProgressChanged:----------->" + newProgress);
                if (newProgress == 100) {

                }
            }


            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                Log.i(TAG, "onReceivedTitle:title ------>" + title);
                if (title.contains("404")){
                  //  showErrorPage();
                }
            }
        });

   //     webParentView = (RelativeLayout) webView.getParent(); //获取父容器
    }

    /**
     * 显示自定义错误提示页面，用一个View覆盖在WebView
     */
//    private void showErrorPage() {
//        webParentView.removeAllViews(); //移除加载网页错误时，默认的提示信息
//        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
//        webParentView.addView(mErrorView, 0, layoutParams); //添加自定义的错误提示的View
//    }

    /***
     * 显示加载失败时自定义的网页
     */
    private void initErrorPage() {
        if (mErrorView == null) {
           // mErrorView = View.inflate(this, R.layout.layout_load_error, null);
        }
    }
}
